package expenager.project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Activities extends Activity{
	LinearLayout linearLayoutActivitiesDate, linearLayoutActivities, linearLayoutActivitiesAmount;
	Button ATButton, ASButton;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activities);
		
		//change to Activities Page
				ATButton = (Button) findViewById(R.id.buttonActivitiesTransaction);
				ATButton.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						//start Activities page activity
						Intent i = new Intent(getApplicationContext(),Transactions.class);
						startActivity(i);
					}
				});
				
				//change to Statistics Page
				ASButton = (Button) findViewById(R.id.buttonActivitiesStatistics);
				ASButton.setOnClickListener(new View.OnClickListener() {
							
					public void onClick(View v) {
						//start Activities page activity
						Intent i = new Intent(getApplicationContext(),Statistics.class);
						startActivity(i);
					}
				});
				
		
		linearLayoutActivitiesDate = (LinearLayout) findViewById(R.id.linearLayoutActivitiesDate);
		linearLayoutActivities = (LinearLayout) findViewById(R.id.linearLayoutActivities);
		linearLayoutActivitiesAmount = (LinearLayout) findViewById(R.id.linearLayoutActivitiesAmount);
		
		//access db and retrieve history
		addHistoryItem();
	}
	
	/* c.getString(i) <-- get data from db
	0=date
	1=month
	2=year
	3=type
	4=category
	5=payment
	6=amount
	7=comment
	*/
	
	public void addHistoryItem() {
		DBHistory db = new DBHistory(this);
		
		db.open();
		Cursor c = db.getHistory();
		db.close();
		
		//trigger when no record found
		if(c.getCount()==0) {
			TextView emptyHistory = new TextView(this);
			emptyHistory.setText("No Transaction history found.");
			emptyHistory.setTextColor(Color.BLACK);
			
			//add textview to linear layout
			linearLayoutActivities.addView(emptyHistory);
		}else {
			//references for dynamic text view
			TextView historyDate[] = new TextView[c.getCount()];
			TextView historyItem[] = new TextView[c.getCount()];
			TextView historyAmount[] = new TextView[c.getCount()];
			
			//create new textview to store history
			for(int i=0;i<c.getCount();i++) {
				TextView tempTextViewDate = new TextView(this);
				TextView tempTextView = new TextView(this);
				TextView tempTextViewAmount = new TextView(this);
				
				//set text...
				
				//set date
				tempTextViewDate.setText(c.getString(0).toString()+"-"+c.getString(1).toString()+"-"+c.getString(2).toString()+"\n\n");
				tempTextViewDate.setTextColor(Color.BLACK);
				linearLayoutActivitiesDate.addView(tempTextViewDate);
				historyDate[i] = tempTextViewDate;
				
				//set item
				tempTextView.setText(c.getString(4).toString()+" - "+c.getString(5).toString()+"\n"+c.getString(7).toString()+"\n");
				tempTextView.setTextColor(Color.BLACK);
				linearLayoutActivities.addView(tempTextView);
				historyItem[i] = tempTextView;
				
				//set amount (check Income or Expense)
				if(c.getString(3).equals("Income")) {
					tempTextViewAmount.setText("+"+c.getString(6).toString()+"\n\n");
					tempTextViewAmount.setTextColor(Color.BLACK);
					linearLayoutActivitiesAmount.addView(tempTextViewAmount);
					historyAmount[i] = tempTextViewAmount;
					
				}else if(c.getString(3).equals("Expense")) {
					tempTextViewAmount.setText("-"+c.getString(6).toString()+"\n\n");
					tempTextViewAmount.setTextColor(Color.BLACK);
					linearLayoutActivitiesAmount.addView(tempTextViewAmount);
					historyAmount[i] = tempTextViewAmount;
				}
				
				//...end of set text
				
				//move pointer to next item
				if(i!=(c.getCount()-1)) {
					c.moveToNext();
					}
			}
		}
	}
}
